//创建Vue实例,得到 ViewModel
var vm = new Vue({
    el: '#app',
    data: {
        books: [{
            id: 1,
            name: "《算法导论》",
            date: '2006-9',
            price: 20,
            count: 1
        }, {
            id: 2,
            name: "《unix编程》",
            date: '2009-9',
            price: 30,
            count: 1
        }, {
            id: 3,
            name: "《重构-改善现有代码设计》",
            date: '2012-9',
            price: 32,
            count: 1
        }, {
            id: 4,
            name: "《人月神话》",
            date: '2003-10',
            price: 64,
            count: 1
        },
        ]
    },
    methods: {
        priceIncert(index) {
            this.books[index].count++
        },
        priceDecert(index) {
            this.books[index].count--
        },
        removeBook(index) {
            this.books.splice(index, 1)
        }
    },
    filters: {
        getprice(price) {
            return '$' + price.toFixed(2);
        }
    },
    computed: {
        totalPrice() {
            // 1.
            // let total = 0;
            // for (let i = 0; i < this.books.length; i++) {
            //     total = total + (this.books[i].price * this.books[i].count)
            // }
            // return total;

            // 2.
            // let total = 0;
            // for (let book in this.books){
            //     total = total + (book.price * book.count)
            // }
            // return total;

            // 3.reduce
            return this.books.map(function(book){return book.price*book.count}).reduce(function(pre ,n ){return pre+n},0)

        }
    }
});